package com.physics.mps.module.comsol.controller;

import cn.hutool.core.io.IoUtil;
import com.physics.mps.common.exception.BusinessException;
import com.physics.mps.entity.vo.ResponseResult;
import com.physics.mps.module.comsol.service.ComsolService;
import com.physics.mps.module.comsol.vo.ComsolSolveVo;
import com.physics.mps.module.comsol.vo.ComsolVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author mcsoft
 * @date 2023/10/16 16:35
 * @description Comsol RestController
 */
@Log4j2
@Api(tags = "Comsol")
@RequiredArgsConstructor
@RestController
@RequestMapping("/comsol")
public class ComsolController {

    private final ComsolService comsolService;


    /**
     * 定制化参数提交
     * （实际未使用）
     * @param comsolSolveVo
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "authorization", value = "authorization", dataType = "String", required = true),
    })
    @PostMapping("/startCompute")
    public ResponseResult startCompute(@RequestBody ComsolSolveVo comsolSolveVo) {
        comsolService.submit(comsolSolveVo);
        return ResponseResult.success();
    }


    /**
     * 结构化参数提交
     * （实际使用）
     * @param comsolVo
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "authorization", value = "authorization", dataType = "String", required = true),
    })
    @PostMapping("/startCompute2")
    public ResponseResult startCompute(@RequestBody ComsolVo comsolVo) {
        comsolService.submit(comsolVo);
        return ResponseResult.success();
    }




    // ~ 文件导出

    @GetMapping("/export/file/{subTaskId}")
    public void exportFile(@PathVariable("subTaskId") String subTaskId, HttpServletResponse response) {
        String path = comsolService.getResult(subTaskId, "file");
        export(false, path, response);
    }

    @GetMapping("/export/img/{subTaskId}")
    public void exportImg(@PathVariable("subTaskId") String subTaskId, HttpServletResponse response) {
        String path = comsolService.getResult(subTaskId, "img");
        export(true, path, response);
    }


    @GetMapping("/export")
    public void export(boolean show, String path, HttpServletResponse response) {
        Path filePath = Paths.get(path);
        try(InputStream inputStream = Files.newInputStream(filePath);
            OutputStream outputStream = response.getOutputStream()) {
            if (!show) {
                response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(filePath.getFileName().toString(), "UTF-8"));
            }
            IoUtil.copy(inputStream, outputStream);
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        }
    }

}
